package com.bw.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bw.lib_base.entity.ParkBeanItem
import com.bw.lib_base.mvvm.BaseViewModel
import com.bw.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService): BaseViewModel() {

    fun getParkByType(para:Any, type: String):Flow<PagingData<ParkBeanItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para, type)
        }).flow.cachedIn(viewModelScope)
    }

    val carNumData = MutableLiveData<ParkBeanItem>()
    fun getParkByCarNum(licensePlate: String){
        httpRequest({apiService.getParkByCarNumber(licensePlate)},carNumData::postValue)
    }

    val addParkSuc = MutableLiveData<ParkBeanItem>()
    fun addPark(item: ParkBeanItem){
        httpRequest({apiService.addPark(toBody(item))},addParkSuc::postValue)
    }

    val detailData = MutableLiveData<ParkBeanItem>()
    fun getParkById(id: Int){
        httpRequest({apiService.detailPark(id)},detailData::postValue)
    }

    val updateParkData = MutableLiveData<ParkBeanItem>()
    fun update(id: Int, body: ParkBeanItem){
        httpRequest({apiService.update(id, body)}, updateParkData::postValue)
    }

    val deleteData = MutableLiveData<Boolean>()
    fun deletePark(id:Int){
        httpRequest({apiService.deletePark(id)},deleteData::postValue)
    }
}